import time
from datetime import datetime
from threading import Timer
import threading
from tkinter import ttk
from tkinter import *

#创建显示窗口
window = Tk()
window.title("日程提醒")

#定义输入时间以及其位置
inputmessageyear = Label(window, text = "年份") #年份
inputmessageyear.grid(column = 0, row = 0)
inputyear = Spinbox(window, from_ = 2022, to=  3000)
inputyear.grid(column = 1,row = 0)

inputmessagemonth = Label(window, text = "月份") #月份
inputmessagemonth.grid(column = 0, row = 1)
inputmonth = Spinbox(window, from_ = 1, to = 12)
inputmonth.grid(column = 1, row = 1)

inputmessageday = Label(window, text = "日") #日
inputmessageday.grid(column = 0, row = 2)
inputday = Spinbox(window, from_ = 1, to = 31)
inputday.grid(column = 1, row = 2)

inputmessagehour = Label(window, text = "小时") #小时
inputmessagehour.grid(column = 0, row = 3)
inputhour = Spinbox(window, from_ = 0, to = 60)
inputhour.grid(column = 1, row = 3)

inputmessageminth = Label(window, text = "分钟") #分钟
inputmessageminth.grid(column = 0, row = 4, )
inputminth = Spinbox(window, from_ = 0, to = 60)
inputminth.grid(column = 1, row = 4)

#输入文本（备忘录事件）
inputmessagetext = Label(window, text = "日程提示")
inputmessagetext.grid(column = 0,row = 5)
inputtext = Entry(window, width = 26)
inputtext.grid(column = 0, row = 6, columnspan = 2, rowspan = 2)

#创建询问函数
def clicked():
    useyear = str(inputyear.get())
    #将输入时间转化为能与当地时间比较的标准格式，此处有些冗余，但懒得再改了
    usemon = str(inputmonth.get())
    useday = str(inputday.get())
    usehour = str(inputhour.get())
    usemin = str(inputminth.get())
    usedatetime = "{}{}{}{}{}{}{}{}{}".format(useyear, "-", usemon, "-", useday, "-", usehour, "-", usemin)
    usedate_time = datetime.strptime(usedatetime, '%Y-%m-%d-%H-%M')
    #此处print用于检测
    """print(usedatetime,"clicked函数年内usedatetime输出正常")"""
    def tishi():
        global t
        benditime = time.localtime(time.time())
        benditime1 = "{}{}{}{}{}{}{}{}{}".format(benditime.tm_year, "-", benditime.tm_mon, "-", benditime.tm_mday, "-", benditime.tm_hour, "-", benditime.tm_min, "-", benditime.tm_sec)
        local_time = datetime.strptime(benditime1, '%Y-%m-%d-%H-%M')
        print("输出benditime")  #测试用
        if local_time != usedate_time:
            print("输出tishi函数内的if输出正常")
            t = Timer(10, tishi)
            t.start()
        elif local_time == usedate_time:
            print("tishi函数内的elif输出正常")
            messageshow = Tk()
            messageshow.title("日程提醒")
            messageshow.geometry('300x300')
            messageshowtext = Label(messageshow, text = inputtext.get()) #获取输入文本信息并显示到新弹窗上
            messageshowtext.grid(column = 0, row = 1)
            messageshow.wm_attributes('-topmost', 1) #将tkinter置顶于最前
            messageshow.mainloop()
    t = threading.Timer(10, tishi)
    t.start()


#添加确定按钮
confirmbutton = Button(window, text = "确定", command = clicked)
confirmbutton.grid(column = 1, row = 8)

window.mainloop()